package study.springboot.redis.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Slf4j
@Configuration
public class RedisCfg {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        log.info(">>>>>>");
        log.info("{}", factory.getClass().getName());
        log.info(">>>>>>");
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(factory);

        //序列化器
        StringRedisSerializer stringSerializer = new StringRedisSerializer();

        //四个序列化配置
        //key
        template.setKeySerializer(stringSerializer);
        //string value
        template.setValueSerializer(stringSerializer);
        //hash key
        template.setHashKeySerializer(stringSerializer);
        //hash value
        template.setHashValueSerializer(stringSerializer);

        //（▲）
        template.setEnableTransactionSupport(false);

        template.afterPropertiesSet();
        return template;
    }
}
